package com.apes.fn.scm.rebatePolicy.model;

import com.alibaba.fastjson.annotation.JSONField;
import com.apes.framework.api.SpringManager;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.plugin.soe.annotations.State;
import com.apes.framework.plugin.soe.annotations.States;
import com.apes.framework.util.DateUtil;
import com.apes.scm.account.account.model.Cooperation;
import com.apes.scm.account.tax.model.Tax;
import com.apes.scm.contract.model.Contract;
import com.apes.scm.masterdata.enumeration.model.Enumeration;
import com.apes.scm.masterdata.finance.model.IncomeType;
import com.apes.scm.masterdata.party.model.PartyRole;
import com.apes.scm.masterdata.party.repository.PartyRoleRepository;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

/**
 * 功能：返利政策
 *
 * @author cyy
 * @time 14:00
 * @date 2023/9/20
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = RebatePolicy.CONST_TABLE_NAME)
public class RebatePolicy extends Model {

    public static final String CONST_TABLE_NAME = "fn_rebate_policy";

    @Id
    @Column(name = "id")
    @ApesGenerator(code = CONST_TABLE_NAME, name = "返利政策编码",prefix = "FG%(platformCompany)",useDateRange=true)
    private String id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "contract_id", referencedColumnName = "id", nullable = false)
    @Comment("合同/协议")
    private Contract contract;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "supplier_id", referencedColumnName = "id")
    @Comment("供应商")
    private PartyRole supplier;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "period_type", referencedColumnName = "id")
    @Comment("返利类型")
    private Enumeration periodType; //001278 月返;001276 季返;001280 年返

    @JSONField(format = DateUtil.SHOW_DATE_FORMAT)
    @Temporal(TemporalType.DATE)
    @Column(name = "start_date")
    @Comment("有效期起")
    private Date startDate;

    @JSONField(format = DateUtil.SHOW_DATE_FORMAT)
    @Temporal(TemporalType.DATE)
    @Column(name = "end_date")
    @Comment("有效期止")
    private Date endDate;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "platform_company_id", referencedColumnName = "id")
    @Comment("平台公司")
    private PartyRole platformCompany;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "income_type_id", referencedColumnName = "id")
    @Comment("费用类型")
    private IncomeType incomeType;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "cooperation_id", referencedColumnName = "id")
    @Comment("合作方式")
    private Cooperation cooperation;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "tax_id", referencedColumnName = "id")
    @Comment("返利税率")
    private Tax tax;

    @Column(name = "note", length = 250)
    @Comment("备注")
    private String note;

    @States(name = "单据状态", values = {
            @State(id = "create", name = "创建"),
            @State(id = "approve", name = "审核"),
            @State(id = "repeal", name = "终止")
    })
    @Column(name = "state", length = 10, nullable = false)
    private String state;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "approve_uid", referencedColumnName = "id")
    @Comment("审核人")
    private PartyRole approveUid;

    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Temporal(TemporalType.DATE)
    @Column(name = "approve_date")
    @Comment("审核日期")
    private Date approveDate;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "repeal_uid", referencedColumnName = "id")
    @Comment("终止人")
    private PartyRole repealUid;

    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "repeal_date")
    @Comment("终止日期")
    private Date repealDate;

    @OneToMany(mappedBy = "rebatePolicy", cascade = CascadeType.ALL, orphanRemoval = true)
    private Set<RebatePolicySupplier> rebatePolicySupplierItems = new HashSet();

    public Set<RebatePolicySupplier> getRebatePolicySupplierItems() {
        if (rebatePolicySupplierItems == null) return new HashSet();
        for (RebatePolicySupplier item : rebatePolicySupplierItems) {
            // 设置主表
            item.setRebatePolicy(this);
        }
        return rebatePolicySupplierItems;
    }

    @OneToMany(mappedBy = "rebatePolicy", cascade = CascadeType.ALL, orphanRemoval = true)
    private Set<RebatePolicyClassify> rebatePolicyClassifyItems = new HashSet();

    public Set<RebatePolicyClassify> getRebatePolicyClassifyItems() {
        if (rebatePolicyClassifyItems == null) return new HashSet();
        for (RebatePolicyClassify item : rebatePolicyClassifyItems) {
            // 设置主表
            item.setRebatePolicy(this);
        }
        return rebatePolicyClassifyItems;
    }

    @OneToMany(mappedBy = "rebatePolicy", cascade = CascadeType.ALL, orphanRemoval = true)
    private Set<RebatePolicyClassifyBrand> rebatePolicyClassifyBrandItems = new HashSet();

    public Set<RebatePolicyClassifyBrand> getRebatePolicyClassifyBrandItems() {
        if (rebatePolicyClassifyBrandItems == null) return new HashSet();
        for (RebatePolicyClassifyBrand item : rebatePolicyClassifyBrandItems) {
            // 设置主表
            item.setRebatePolicy(this);
        }
        return rebatePolicyClassifyBrandItems;
    }

    @OneToMany(mappedBy = "rebatePolicy", cascade = CascadeType.ALL, orphanRemoval = true)
    private Set<RebatePolicyProduct> rebatePolicyProductItems = new HashSet();

    public Set<RebatePolicyProduct> getRebatePolicyProductItems() {
        if (rebatePolicyProductItems == null) return new HashSet();
        for (RebatePolicyProduct item : rebatePolicyProductItems) {
            // 设置主表
            item.setRebatePolicy(this);
        }
        return rebatePolicyProductItems;
    }

    @Transient
    private PartyRole _creator;
    @Transient
    public PartyRole get_creator() {
        if(this.getCreator() == null) return null;
        return SpringManager.getBean(PartyRoleRepository.class).findOne(this.getCreator());
    }
}
